package com.UserModule.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.configuration.XMLConfiguration;

import util.ConnectDB;
import util.UtilDAO;

import com.UserModule.bean.TbUserBean;
import com.UserModule.dao.TbUserDAO;


public class TbUsersServlet extends HttpServlet{
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = "";
		if (request.getParameter("method") != null) {
			method = request.getParameter("method");
		}
		if ("add".equals(method)) {
			String userName=request.getParameter("userName");
			String userPass=request.getParameter("userPass");
			String biaoshi=request.getParameter("biaoshi");
			if(new TbUserDAO().add(new TbUserBean(userName,userPass,Integer.parseInt(biaoshi))))
			{
				request.getSession().setAttribute("userModuleResult", "添加用户成功");
			}else{
				request.getSession().setAttribute("userModuleResult", "添加用户失败");
			}
			response.sendRedirect("UserModule/UsersList.jsp");			
		}//end method add

		if ("edit".equals(method)) {
			int userId=Integer.parseInt(request.getParameter("userId"));
			String userName=request.getParameter("userName");
			String userPass=request.getParameter("userPass");
			if(new TbUserDAO().edit(new TbUserBean(userId,userName,userPass))){
				request.getSession().setAttribute("userModuleResult", "修改用户成功");
			}else{
				request.getSession().setAttribute("userModuleResult", "修改用户失败");				
			}
			response.sendRedirect("UserModule/UsersList.jsp");

		}
		if("login".equals(method)){
			String userName=request.getParameter("userName");
			String userPass=request.getParameter("userPass");
			TbUserBean tu=new TbUserDAO().checkLogin(userName,userPass);
			request.getSession().setAttribute("User", tu);
			if(tu!=null){
				response.sendRedirect("index.jsp?err=1");
			}else{
				response.sendRedirect("UserModule/Login.jsp?err=1");
			}
		}
		
		 
		
	}// end method processRequest

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.processRequest(request, response);
	}// end method doGet;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.processRequest(request, response);
	}// end method doPost

}
